package com.lagou.util;

import com.lagou.annotion.Autowired;
import com.lagou.annotion.Comppent;
import com.lagou.config.Config;
import org.apache.ibatis.session.SqlSession;

/**
 * @Authther: mrwu
 * @date:2021/3/13-03
 * @Description:com.lagou.util
 */
@Comppent
public class SqlsssionUtils {
     private ThreadLocal<SqlSession> threadLocal=new ThreadLocal<>();
     @Autowired
     private Config config;

    public SqlsssionUtils() {
    }
    //     -------------------单例模式开始---------------
    //     private static SqlsssionUtils sqlsssionUtils = new SqlsssionUtils();
    //    private SqlsssionUtils() {
//    }
//    public static SqlsssionUtils getInstance(){
//        return sqlsssionUtils;
//    }
    //--------------------单例模式结束----------------
//    ----------IOC-----------

    public void setConfig(Config config) {
        this.config = config;
    }

    public SqlSession getCurrentSqlsssion(){
         //将sqlsession 绑定到线程中，并从中取出
         if (threadLocal.get()==null){
             SqlSession sqlSession = config.getSqlSession();
             threadLocal.set(sqlSession);
         }
         return threadLocal.get();
     }

}
